﻿Public Class NV_Danhsach
    Private nvBL As NhanVienBL
    Private nvDT As NhanVienDT

    Private Sub loadDGV(ByVal dieukien As String)
        dgv.ClearSelection()
        nvBL = New NhanVienBL()
        Dim list As New List(Of NhanVienDT)
        list = nvBL.GetData(dieukien)
        dgv.DataSource = list
    End Sub
    Private Sub sua()
        If dgv.SelectedRows.Count > 0 Then
            Dim s As New NV_Nhap()
            s.flagIsUp = False
            s.MaNV = dgv.SelectedRows(0).Cells(0).Value
            s.hotenNV = dgv.SelectedRows(0).Cells(1).Value
            s.gioitinh = dgv.SelectedRows(0).Cells(2).Value
            s.ngaysinh = Convert.ToDateTime(dgv.SelectedRows(0).Cells(3).Value)
            s.dienthoai = dgv.SelectedRows(0).Cells(4).Value
            s.email = dgv.SelectedRows(0).Cells(5).Value
            s.diachi = dgv.SelectedRows(0).Cells(6).Value
            s.trinhdo = dgv.SelectedRows(0).Cells(7).Value
            s.hesoluong = dgv.SelectedRows(0).Cells(8).Value
            s.Id = dgv.SelectedRows(0).Cells(9).Value
            s.ShowDialog()
            loadDGV("")
        Else
            Return
        End If
    End Sub

    Private Sub NV_Danhsach_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        loadDGV("")
    End Sub

    Private Sub btnThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThem.Click
        Dim t As New NV_Nhap()
        t.flagIsUp = True
        t.ShowDialog()
        loadDGV("")
    End Sub

    Private Sub btnXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoa.Click
        If dgv.SelectedRows.Count > 0 Then
            If MessageBox.Show("Bạn có chắc muốn xóa nhân viên <" + dgv.SelectedRows(0).Cells(2).Value.ToString() + "> nvông ?", "Xóa thông báo", MessageBoxButtons.YesNo) = MsgBoxResult.Yes Then
                nvDT = New NhanVienDT(Convert.ToInt32(dgv.SelectedRows(0).Cells(0).Value))
                nvBL = New NhanVienBL()
                nvBL.Delete(nvDT)
                MessageBox.Show("Xóa thành công")
                loadDGV("")
            Else
                MsgBox("Đã hủy thao tác xóa!", vbExclamation, "Thông báo")
            End If
        End If
    End Sub

    Private Sub btnSua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSua.Click
        sua()
    End Sub

    Private Sub btnAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAll.Click
        loadDGV("")
    End Sub

    Private Sub txtTim_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtTim.TextChanged
        loadDGV(" SELECT * FROM NhanVien Where hotenNV LIKE N'%" + txtTim.Text + "%' OR MaNV LIKE '%" + txtTim.Text + "%'")
    End Sub

    Private Sub txtTim_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtTim.KeyDown
        If e.KeyCode = Keys.Enter Then
            loadDGV(" SELECT * FROM NhanVien Where hotenNV LIKE N'%" + txtTim.Text + "%' OR MaNV LIKE '%" + txtTim.Text + "%'")
        End If
    End Sub

    Private Sub dgv_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgv.DoubleClick
        sua()
    End Sub
End Class
